/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package co.edu.unal.agentes;

import co.edu.unal.behaviours.CheckStockServer;
import co.edu.unal.behaviours.CreateStock;
import co.edu.unal.behaviours.OfferRequestsServer;
import co.edu.unal.behaviours.PurchaseOrdersServer;
import co.edu.unal.gui.BookDistributorGui;
import co.edu.unal.inventory.Stock;
import jade.core.Agent;
import jade.core.behaviours.OneShotBehaviour;
import jade.domain.DFService;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import jade.domain.FIPAException;
import java.util.Date;
import java.util.HashMap;
import java.util.Random;

/**
 *
 * @author Camilo
 */
public class BookDistributorAgent extends Agent{
    private HashMap catalogue;
    private BookDistributorGui myGui;
    private Stock stock;
    
    @Override
    protected void setup() {
        //Creando gui
        myGui = new BookDistributorGui(this);
        myGui.showGui();
        //Creando Inventario
        stock = new Stock(100,10,1);
        addBehaviour(new CreateStock(stock,myGui.getLog()));
        // Printout a welcome message
        myGui.getLog().setText("Hello! Distributor-agent " + getAID().getLocalName() + " is ready.");
        catalogue = new HashMap();
        Random r=new Random(new Date().getTime());
        catalogue.put("Estudio en escarlata", 80+r.nextInt(20));
        addBehaviour(new CheckStockServer(catalogue, stock,myGui.getLog(),80+r.nextInt(20),"book-distributor",myGui.getLista()));
        addBehaviour(new OfferRequestsServer(catalogue));
        addBehaviour(new PurchaseOrdersServer(catalogue,"book-distributor",myGui.getLog()));
        DFAgentDescription dfd = new DFAgentDescription();
        dfd.setName(getAID());
        ServiceDescription sd = new ServiceDescription();
        sd.setType("book-distributor");
        sd.setName("JADE-book-trading");
        dfd.addServices(sd);
        try {
            DFService.register(this, dfd);
        } catch (FIPAException fe) {
            myGui.getLog().setText(myGui.getLog()+"\n"+"fe.getMessage() = " + fe.getMessage());
        }
    }
    
    @Override
    protected void takeDown() {
        // Deregister from the yellow pages
        try {
            DFService.deregister(this);
        } catch (FIPAException fe) {
            myGui.getLog().setText(myGui.getLog()+"\n"+"fe.getMessage() = " + fe.getMessage());
        }
        myGui.getLog().setText(myGui.getLog()+"\n"+"Distributor-agent " + getAID().getLocalName() + " terminating.");
    }
    
    public void updateCatalogue(final String title, final int price) {
        catalogue.put(title, new Integer(price));
//        addBehaviour(new OneShotBehaviour() {
//            @Override
//            public void action() {
//                catalogue.put(title, new Integer(price));
//            }
//        });
    }

    public Stock getInventario() {
        return stock;
    }

    public void setInventario(Stock inventario) {
        this.stock = inventario;
    }
}
